package cn.yh.register.hosp.service.impl;

import cn.yh.register.hosp.repository.HospitalRepository;
import cn.yh.register.hosp.service.HospitalService;
import cn.yh.register.model.hosp.Hospital;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    HospitalRepository hospitalRepository;

    /**
     * 保存医院信息
     * @param objectMap 参数信息
     */
    @Override
    public void savaHospital(Map<String, Object> objectMap) {
        //转对象
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(objectMap), Hospital.class);
        //先查询数据库是否已存在
        Hospital hosp = hospitalRepository.findByHoscode(hospital.getHoscode());
        if (hosp==null) {
            //不存在，新增
            hospital.setStatus(0);//0:未上线  1:已上线
            hospital.setCreateTime(new Date());
            hospital.setIsDeleted(0);
        }else {
            //存在，修改
            hospital.setId(hosp.getId());
            hospital.setCreateTime(hosp.getCreateTime());
            hospital.setIsDeleted(hosp.getIsDeleted());
            hospital.setStatus(hosp.getStatus());
        }
        hospital.setUpdateTime(new Date());
        //保存
        hospitalRepository.save(hospital);
    }

    /**
     * 查询医院对象
     * @param hoscode 医院编号
     * @return 医院对象
     */
    @Override
    public Hospital findHospital(String hoscode) {
        return hospitalRepository.findByHoscode(hoscode);
    }
}
